// Server API makes it possible to hook into various parts of Gridsome
// on server-side and add custom data to the GraphQL data layer.
// Learn more: https://gridsome.org/docs/server-api/

// Changes here require a server restart.
// To restart press CTRL + C in terminal and run `gridsome develop`
const nodeExternals = require('webpack-node-externals');
// const VuetifyLoaderPlugin = require('vuetify-loader/lib/plugin');
module.exports = function (api) {
    // api.chainWebpack((config, { isServer }) => { config.plugin('vuetify-loader').use(VuetifyLoaderPlugin); });
    api.chainWebpack((config, {isServer}) => {
        if (isServer) {
            config.externals([
                nodeExternals({
                    whitelist: [/^Vuetify/, /^vuetify/]
                })
            ]);
        }
    });
    api.loadSource(({addCollection}) => {
        // Use the Data Store API here: https://gridsome.org/docs/data-store-api/
    })
    // api.createPages(({
    //                      createPage
    //                  }) => {
    //     // Use the Pages API here: https://gridsome.org/docs/pages-api/
    // })
    // api.createPages(async ({graphql, createPage}) => {
    //     const {data} = await graphql(`{
    //   KuERP {
    //     allKaikongHardwareProductTemplate(category:"板卡") {
    //         id
    //         name
    //     }
    //   }
    // }`)
    //
    //     data.KuERP.allKaikongHardwareProductTemplate.forEach(function (node) {
    //         //console.info(node.id)
    //         createPage({
    //             path: `/product/${node.id}`,
    //             component: './src/templates/Tproduct.vue',
    //             context: {
    //                 id: node.id,
    //                 title: 'bbb'
    //             }
    //
    //         })
    //     })
    // })

    api.createManagedPages(async ({graphql, createRoute}) => {
        const customerRoute = createRoute({
            path: `/product/:id(\\d+)`,
            component: './src/templates/HwProduct.vue'
        })
        const {data} = await graphql(`{
      KuERP {
        allKaikongHardwareProductTemplate{
            id
            name
        }
      }
    }`)
        //console.info('3')
        data.KuERP.allKaikongHardwareProductTemplate.forEach(item => {
            //console.info(item)
            customerRoute.addPage({
                path: `/product/${item.id}`,
                context: {
                    domain: JSON.stringify([['id', '=', item.id]]),
                    title: item.name
                }
            })
        })
    })
    api.createManagedPages(async ({graphql, createRoute}) => {
        const customerRoute = createRoute({
            path: `/soft/:id(\\d+)`,
            component: './src/templates/Swproduct.vue'
        })
        const {data} = await graphql(`{
      KuERP{
        allKtSwPdtTpl {
            id
            name
        }
      }
    }`)
        //console.info('3')
        data.KuERP.allKtSwPdtTpl.forEach(item => {
            //console.info(item)
            customerRoute.addPage({
                path: `/soft/${item.id}`,
                context: {
                    id: item.id,
                    title: item.name
                }
            })
        })
    })
    //console.info('4')
    api.createManagedPages(async ({graphql, createRoute}) => {
        const customerRoute = createRoute({
            path: `/pservice/:id(\\d+)`,
            component: './src/templates/Serive.vue'
        })
        const {data} = await graphql(`{
      KuERP {
        allDevService {
            id
            name
            topic
            content
        }
      }
    }`)
        //console.info('5')
        data.KuERP.allDevService.forEach(item => {
            //console.info(item)
            customerRoute.addPage({
                path: `/pservice/${item.id}`,
                context: {
                    id: item.id,
                }
            })
        })
    })
    api.createManagedPages(async ({graphql, createRoute}) => {
        const customerRoute = createRoute({
            path: `/content/:id(\\d+)`,
            component: './src/templates/Content.vue'
        })
        const {data} = await graphql(`{
      KuERP {
        allContentContent(domain:"[[\\"public\\",\\"=\\",\\"True\\"]]") {
            id
        }
      }
    }`)
        data.KuERP.allContentContent.forEach(item => {
            console.info(item)
            customerRoute.addPage({
                path: `/content/${item.id}`,
                context: {
                    domain: JSON.stringify([["id", "=", item.id]]),
                }
            })
        })
    })
    //console.info('6')
    api.createManagedPages(async ({graphql, createRoute}) => {
        const customerRoute = createRoute({
            path: `/hwcat/:id(\\d+)`,
            component: './src/pages/hw.vue',
        })
        //console.info('61')
        const {data} = await graphql(`{
      KuERP {
        allProductCategory(domain:"[[\\"parent_id.name\\",\\"=\\",\\"硬件产品\\"]]") {
            id
            name
        }
      }
    }`)
        //console.info('7')
        data.KuERP.allProductCategory.forEach(item => {
            console.info(item)
            customerRoute.addPage({
                path: `/hwcat/${item.id}`,
                context: {
                    id: item.id,
                    title: item.name,
                    domain: JSON.stringify([['categ_id.name', '=', item.name]])
                }
            })
        })
    })
    api.createManagedPages(async ({graphql, createRoute}) => {
        const customerRoute = createRoute({
            path: `/folder/:id(\\d+)`,
            component: './src/pages/content.vue',
        })
        //console.info('61')
        const {data} = await graphql(`{
      KuERP {
        allContentFolder(domain:"[[\\"public\\",\\"=\\",\\"True\\"]]") {
            id
            name
        }
      }
    }`)
        //console.info('7')
        data.KuERP.allContentFolder.forEach(item => {
            //console.info(item)
            customerRoute.addPage({
                path: `/folder/${item.id}`,
                context: {
                    id: item.id,
                    title: item.name,
                    domain: JSON.stringify([['folder_id.name', '=', item.name], ["public", "=", "True"]])
                }
            })
        })
    })
    api.createManagedPages(async ({graphql, createRoute}) => {
        const customerRoute = createRoute({
            path: `/ptech/:id(\\d+)`,
            component: './src/templates/Tech.vue'
        })
        const {data} = await graphql(`{
    KuERP{
    allKtMarketTactics {
    name
    id
    }}
    }`)
        //console.info('5')
        data.KuERP.allKtMarketTactics.forEach(item => {
            //console.info(item)
            customerRoute.addPage({
                path: `/ptech/${item.id}`,
                context: {
                    domain: JSON.stringify([["id", "=", item.id]]),
                }
            })
        })
    })


}
